package com.example.myapplication.test;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.example.myapplication.TimeTextView;

import java.util.List;

/**
 * @author 饶忠俊
 * 2019/12/24 0024
 * email：markraostudio@gmail.com
 * description：{ 时间倒计时 Handler }
 */
public class CountDownHandler extends Handler {
    /**
     * 课程订单详情
     */
    public static final int PAGE_COURSE_ORDER_DETAILS = 0;
    /**
     * 课程详情
     */
    public static final int PAGE_COURSE_DETAILS = 1;
    private List<TimeTextView> mTextViews;
    private TimeTextView mTimeTextView;

    public CountDownHandler(List<TimeTextView> mTextViews) {
        super(Looper.getMainLooper());
        this.mTextViews = mTextViews;
    }

    public CountDownHandler(TimeTextView mTextView) {
        super(Looper.getMainLooper());
        this.mTimeTextView = mTextView;
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
            case PAGE_COURSE_DETAILS:
                for (int i = 0, y = mTextViews.size() - 1; i <= y; y--) {
                    // 时间到期，从队列移除
                    if (mTextViews.get(y).updateCourseDetailsTime()) {
                        mTextViews.remove(i);
                    }
                }
                sendEmptyMessageDelayed(PAGE_COURSE_DETAILS, 1000);
                break;
            case PAGE_COURSE_ORDER_DETAILS:
                // 没有过期，才继续发延时任务
                if (!mTimeTextView.updateCourseOrderDetailsTime()) {
                    sendEmptyMessageDelayed(PAGE_COURSE_ORDER_DETAILS, 1000);
                }
                break;
            default:
                break;
        }
    }

}
